// 事件处理模块

// 拖放相关事件处理
export function handleDragOver(e) {
    e.preventDefault();
    e.dataTransfer.dropEffect = 'move';
}

// 模态框事件处理已移至modalManager.js

// 图片上传相关事件处理
export function setupImageUploadEvents() {
    console.log('开始设置图片上传事件');
    
    // 清除所有之前的事件监听器
    const elementsToClone = ['upload-button', 'upload-image', 'change-image', 'crop-close', 'crop-confirm'];
    
    elementsToClone.forEach(id => {
        const element = document.getElementById(id);
        if (element) {
            // 克隆并替换元素以移除所有事件监听器
            const newElement = element.cloneNode(true);
            if (element.parentNode) {
                element.parentNode.replaceChild(newElement, element);
            }
        }
    });
    
    // 重新获取新的干净元素
    const uploadButton = document.getElementById('upload-button');
    const uploadInput = document.getElementById('upload-image');
    const changeImageBtn = document.getElementById('change-image');
    const cropCloseBtn = document.getElementById('crop-close');
    const cropConfirmBtn = document.getElementById('crop-confirm');
    
    console.log('重置后的元素状态:', {
        uploadButton: !!uploadButton,
        uploadInput: !!uploadInput,
        changeImageBtn: !!changeImageBtn,
        cropCloseBtn: !!cropCloseBtn,
        cropConfirmBtn: !!cropConfirmBtn
    });
    
    // 图标类型切换
    const iconTypeRadios = document.querySelectorAll('input[name="icon-type"]');
    console.log('找到图标类型单选按钮:', iconTypeRadios.length);
    
    iconTypeRadios.forEach(radio => {
        radio.addEventListener('change', () => {
            const type = radio.value;
            console.log('选择了图标类型:', type);
            // showIconOptions函数应该在main.js中定义
            if (typeof showIconOptions !== 'undefined') {
                showIconOptions(type);
            }
            
            // 如果选择了图片模式，强制显示图片上传区域
            if (type === 'image') {
                console.log('选择了图片模式，强制显示上传区域');
                // toggleImageUpload函数应该在main.js中定义
                if (typeof toggleImageUpload !== 'undefined') {
                    toggleImageUpload(true);
                }
            }
        });
    });
    
    // 上传图片按钮 - 使用直接赋值方式而不是addEventListener
    if (uploadButton && uploadInput) {
        uploadButton.onclick = function(e) {
            e.preventDefault();
            e.stopPropagation();
            console.log('点击上传按钮');
            uploadInput.click();
        };
        
        uploadInput.onchange = function(e) {
            console.log('选择了文件');
            if (e.target.files && e.target.files[0]) {
                const file = e.target.files[0];
                console.log('文件类型:', file.type, '文件大小:', file.size);
                console.log('打开裁剪模态框');
                // openCropModal函数应该在main.js中定义
                if (typeof openCropModal !== 'undefined') {
                    openCropModal(file);
                }
                
                // 阻止事件传播
                if (e.stopPropagation) e.stopPropagation();
                if (e.preventDefault) e.preventDefault();
                return false;
            }
        };
    }
    
    // 更换图片按钮 - 使用直接赋值
    if (changeImageBtn && uploadInput) {
        changeImageBtn.onclick = function(e) {
            e.preventDefault();
            e.stopPropagation();
            console.log('点击更换图片按钮');
            uploadInput.click();
        };
    }
    
    // 裁剪模态框按钮 - 使用直接赋值
    if (cropCloseBtn) {
        cropCloseBtn.onclick = function(e) {
            e.preventDefault();
            e.stopPropagation();
            console.log('点击关闭裁剪模态框按钮');
            // closeCropModal函数应该在main.js中定义
            if (typeof closeCropModal !== 'undefined') {
                closeCropModal();
            }
        };
    }
    
    if (cropConfirmBtn) {
        cropConfirmBtn.onclick = function(e) {
            e.preventDefault();
            e.stopPropagation();
            console.log('点击确认裁剪按钮');
            // confirmCrop函数应该在main.js中定义
            if (typeof confirmCrop !== 'undefined') {
                confirmCrop();
            }
        };
    }
}